package com.online.onlineshop.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.online.onlineshop.entity.Cart;
import com.online.onlineshop.mapper.CartMapper;
import com.online.onlineshop.mapper.CartMapperVOMapper;
import com.online.onlineshop.mapper.ProductionMapper;
import com.online.onlineshop.services.ForCartService;
import com.online.onlineshop.vo.CartValueObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ForCartServiceImpl implements ForCartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    CartMapperVOMapper cartMapperVOMapper;

    @Autowired
    ProductionMapper productionMapper;

    @Autowired
    ForCartService cartService;

    private Integer uid;

    private Integer[] cids;

    /**
     * @param uid 当前登录用户的uid
     * @return 返回用户购物车中的数据
     */
    @Override
    public List<CartValueObject> getCartVOByUid(Integer uid) {
        List<CartValueObject> cartVO = cartMapper.getCartVOByUid(uid);
        return cartVO;
    }

    @Override
    public Integer deleteByCid(int cid) {
        Integer rs = cartMapper.deleteById(cid);
        if (rs != 1) {
            throw new RuntimeException("删除数据出错");
        }
        return rs;
    }

    @Override
    public Integer deleteByCids(Integer[] cid) {
        return cartMapper.deleteBatchIds(Arrays.asList(cid));
    }

    /*
     *
     * 显示用户勾选的购物车商品数据
     *
     * */
    @Override
    public List<CartValueObject> findbycids(Integer uid, String cidr) {
        String[] strArr = cidr.split(",");
        Integer[] tcids = new Integer[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            tcids[i] = Integer.valueOf(strArr[i]);
        }
        this.uid = uid;
        this.cids = tcids;
        List<CartValueObject> lists = cartMapperVOMapper.findByCids(cids);
        Iterator<CartValueObject> iterator = lists.iterator();
        //过滤商品，只保留当前用户
        while (iterator.hasNext()) {
            CartValueObject co = iterator.next();
            if (co.getUid() != uid) {
                lists.remove(co);

            }
        }
        return lists;
    }
    //


    @Override
    public Integer addNum(Integer cid, Integer uid, String userName) {

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getCid, cid);
        Cart rs = cartMapper.selectOne(wrapper);

        if (rs == null) {
            throw new RuntimeException("用户数据不存在");
        }
        if (rs.getUid() != uid) {
            throw new RuntimeException("用户无权限");
        }

        Integer num = rs.getNum() + 1;
        rs.setNum(num);
        Date date = new Date();
        int updateRow = cartMapper.updateById(rs);

        if (updateRow != 1) {
            throw new RuntimeException("更新数据出错");
        }
        return num;
    }

    @Override
    public Integer subNum(Integer cid, Integer uid, String userName) {

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getCid, cid);
        Cart rs = cartMapper.selectOne(wrapper);

        if (rs == null) {
            throw new RuntimeException("用户数据不存在");
        }
        if (rs.getUid() != uid) {
            throw new RuntimeException("用户无权限");
        }

        Integer num = rs.getNum() - 1;
        if (num < 0) {
            throw new RuntimeException("数量不可小于0");
        }
        rs.setNum(num);
        Date date = new Date();
        int updateRow = cartMapper.updateById(rs);

        if (updateRow != 1) {
            throw new RuntimeException("更新数据出错");
        }
        return num;
    }

    /*
     * 加入购物车的业务方法
     * */
    @Override
    public Integer putToCart(Integer uid, Integer pid, Integer amount, String userName) {
        //通过pid查询购物车中是否含有当前商品
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getPid, pid);
        Cart rs = cartMapper.selectOne(wrapper);

        int[] arr = new int[2];
        if (rs == null) {
            Cart cart = new Cart();
            arr = insertCart(cart, uid, pid, amount, userName);
            if (arr[0] != 1) {
                throw new RuntimeException("增加购物车商品产生异常");
            }
        } else if (rs != null && uid != rs.getUid()) {
            Cart cart = new Cart();
            arr = insertCart(cart, uid, pid, amount, userName);
            if (arr[0] != 1) {
                throw new RuntimeException("增加购物车商品产生异常");
            }
        } else if (rs != null) {
            Integer updateNum = rs.getNum() + amount;
            rs.setNum(updateNum);
            int i = cartMapper.updateById(rs);
            arr[1] = rs.getCid();
            if (i != 1) {
                throw new RuntimeException("更新购物车商品产生异常");
            }
        }
        return arr[1];
    }

    private int[] insertCart(Cart cart, Integer uid, Integer pid, Integer amount, String userName) {
        Date date = new Date();
        cart.setUid(uid);
        cart.setPid(pid);
        cart.setNum(amount);
        cart.setCreatedUser(userName);
        cart.setCreatedTime(date);
        cart.setModifiedTime(date);
        cart.setModifiedUser(userName);
        int rows = cartMapper.insert(cart);
        return new int[]{rows, cart.getCid()};
    }
}


